package com.haolijun.eecms.controller;

import cn.hutool.json.JSONUtil;
import com.haolijun.eecms.common.R;
import com.haolijun.eecms.controller.form.InsertNoticeForm;
import com.haolijun.eecms.controller.form.SearchNoticeByPageForm;
import com.haolijun.eecms.controller.form.UpdateNoticeForm;
import com.haolijun.eecms.entity.TbNotice;
import com.haolijun.eecms.service.NoticeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;

/**
 * @ClassName NoticeController
 * @Description TODO
 * @Author hljstart
 * @Date 2023/3/11 12:16
 * @Version 1.0
 */
@RestController
@RequestMapping("/notice")
@Tag(name = "NoticeController",description = "公告Web接口")
public class NoticeController {

    @Autowired
    private NoticeService noticeService;
    /**
     * 查询公告分页信息
     * @param form
     * @return
     */
    @PostMapping("/searchNoticeByPage")
    @Operation(summary = "查询公告分页信息")
    public R searchNoticeByPage(@Valid @RequestBody SearchNoticeByPageForm form){
        HashMap param = JSONUtil.parseObj(form).toBean(HashMap.class);
        R r = noticeService.searchNoticeByPage(param);
        return r;
    }
    /**
     * 查询公告详情
     * @param nid
     * @return
     */
    @GetMapping("/searchNoticeById/{nid}")
    @Operation(summary = "查询公告信息信息(带评论)")
    public R searchNoticeById(@PathVariable("nid") Integer nid){
        HashMap noticeMap = noticeService.searchNoticeById(nid);
        return R.ok().data(noticeMap);
    }
    /**
     * 查看公告信息，用于修公告数据回显
     * @param nid
     * @return
     */
    @GetMapping("/searchNoticeInfo/{nid}")
    @Operation(summary = "查看公告信息，用于修公告数据回显")
    public R searchNoticeInfo(@PathVariable("nid") Integer nid){
        HashMap noticeMap = noticeService.searchNoticeInfo(nid);
        return R.ok().data(noticeMap);
    }

    @PostMapping("/insert")
    @Operation(summary = "添加公告")
    public R insert(@Valid @RequestBody InsertNoticeForm form){
        TbNotice notice = JSONUtil.parse(form).toBean(TbNotice.class);
        int rows = noticeService.insert(notice);
        return rows > 0 ? R.ok() : R.error();
    }
    @PostMapping("/update")
    @Operation(summary = "更新公告")
    public R update(@Valid @RequestBody UpdateNoticeForm form){
        TbNotice notice = JSONUtil.parse(form).toBean(TbNotice.class);
        int rows = noticeService.update(notice);
        return rows > 0 ? R.ok() : R.error();
    }
    @GetMapping("/deleteById/{nid}")
    @Operation(summary = "删除公告")
    public R deleteById(@PathVariable("nid") Integer nid){
        int rows = noticeService.deleteNoticeById(nid);
        return rows > 0 ? R.ok() : R.error();
    }

    @GetMapping("/likeNotice/{nid}")
    @Operation(summary = "公告点赞功能")
    public R likeNotice(@PathVariable("nid") Integer nid){
        R r = noticeService.likeNotice(nid);
        return r;
    }


}
